package com.zhijian.warehouse.manage.service.impl;

import cn.hutool.core.date.DateUtil;
import com.zhijian.util.BeanUtil;
import com.zhijian.warehouse.manage.entity.vo.request.AccessProductRecordSearchVo;
import com.zhijian.warehouse.manage.entity.vo.request.StoreOptRecordSearchVo;
import com.zhijian.warehouse.manage.entity.vo.response.AccessProductRecordVo;
import com.zhijian.warehouse.manage.entity.vo.response.IndexOutlineVo;
import com.zhijian.warehouse.manage.entity.vo.response.StoreOptRecordVo;
import com.zhijian.warehouse.manage.enums.ProductEnum;
import com.zhijian.warehouse.manage.enums.StoreEnum;
import com.zhijian.warehouse.manage.service.AccessProductRecordService;
import com.zhijian.warehouse.manage.service.IndexService;
import com.zhijian.warehouse.manage.service.StoreOptRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 品类业务处理层
 *
 * @author auto
 * @version: 1.0
 * @date 2024-10-10 20:08:21
 */
@Slf4j
@Service
public class IndexServiceImpl implements IndexService {
    @Autowired
    private StoreOptRecordService storeOptRecordService;
    @Autowired
    private AccessProductRecordService accessProductRecordService;

    @Override
    public IndexOutlineVo selectLineVo() {
        //今天0点时间字符串
        String startTimeStr = String.format("%s 00:00:00", DateUtil.format(new Date(), "yyyy-MM-dd"));
        //今天24点时间字符串
        String endTimeStr = String.format("%s 23:59:59", DateUtil.format(new Date(), "yyyy-MM-dd"));

        //查询今日出入库操作总数
        List<StoreOptRecordVo> storeOptRecordVos = storeOptRecordService.selectList(StoreOptRecordSearchVo.builder()
                .optStartTimeStr(startTimeStr)
                .optEndTimeStr(endTimeStr)
                .build());
        IndexOutlineVo indexOutlineVo = IndexOutlineVo.builder()
                .giveBackCount(BigDecimal.ZERO)
                .receiveCount(BigDecimal.ZERO)
                .inStore(BigDecimal.ZERO)
                .outStore(BigDecimal.ZERO)
                .build();
        if (BeanUtil.isNotEmpty(storeOptRecordVos)) {
            Map<Integer, BigDecimal> totalCountsByType = storeOptRecordVos.stream()
                    .filter(Objects::nonNull)// 过滤掉可能的 null 值
                    .filter(record -> record.getCount() != null) // 过滤掉 count 为 null 的记录
                    .collect(Collectors.groupingBy(
                            StoreOptRecordVo::getType, // 根据 type 分组
                            Collectors.mapping(StoreOptRecordVo::getCount, Collectors.reducing(BigDecimal.ZERO, BigDecimal::add)) // 累加 count
                    ));
            //入库统计
            indexOutlineVo.setInStore(totalCountsByType.getOrDefault(StoreEnum.IN_OUT_TYPE.IN.getCode(), BigDecimal.ZERO)); // 获取类型为 1 的累计值
            //出库统计
            indexOutlineVo.setOutStore(totalCountsByType.getOrDefault(StoreEnum.IN_OUT_TYPE.OUT.getCode(), BigDecimal.ZERO)); // 获取类型为 -1 的累计值
        }
        //查询领用
        List<AccessProductRecordVo> accessProductRecordVos = accessProductRecordService.selectList(AccessProductRecordSearchVo.builder()
                .startTimeStr(startTimeStr)
                .endTimeStr(endTimeStr)
                .build());

        if (BeanUtil.isNotEmpty(accessProductRecordVos)){
            // 领用总数
            Integer ly = accessProductRecordService.countDrawBack(AccessProductRecordSearchVo.builder()
                    .startTimeStr(startTimeStr)
                    .endTimeStr(endTimeStr)
                    .type(1)   //1领用
                    .build());
            if (BeanUtil.isNotEmpty(ly))
                indexOutlineVo.setReceiveCount(new BigDecimal(ly));
            // 归还总数
            Integer gh = accessProductRecordService.countDrawBack(AccessProductRecordSearchVo.builder()
                    .startTimeStr(startTimeStr)
                    .endTimeStr(endTimeStr)
                    .type(2)   //2归还
                    .build());
            if (BeanUtil.isNotEmpty(gh))
                indexOutlineVo.setGiveBackCount(new BigDecimal(gh));
        }
        return indexOutlineVo;
    }
}